package vn.student.controller;

import java.util.ArrayList;
import vn.student.AStudentDB;
import vn.student.R;
import vn.student.ViewAssignmentDetail;
import vn.student.mylib.Assignment;
import vn.student.mylib.Class;
import vn.student.mylib.ClassType;
import vn.student.mylib.Constants;
import vn.student.mylib.ListAssignmentAdapter;
import vn.student.mylib.WeekDay;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnCreateContextMenuListener;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class ViewClassDetailController {
	Activity act;
	AStudentDB myDB = null;
	String id;
	public Class classInfo = null;
	public ViewClassDetailController(Activity act){
		this.act = act;
		myDB = new AStudentDB(act); 
	}
	public void showClassDetail(String id){
		this.id = id;
		classInfo = myDB.getClassInfo(id);
		if(classInfo!=null){
			TextView label = (TextView)act.findViewById(R.id.classNameLabel);
			label.setText(classInfo.className);
			label = (TextView)act.findViewById(R.id.typeLabel);
			label.setText(ClassType.getName(classInfo.type));
			label = (TextView)act.findViewById(R.id.locationLabel);
			label.setText(classInfo.location);
			label = (TextView)act.findViewById(R.id.instructorNameLabel);
			label.setText(classInfo.instructorName);
			label = (TextView)act.findViewById(R.id.timeLabel);
			ImageView icon = (ImageView) act.findViewById(R.id.classIcon);
			icon.setImageResource(ClassType.getIconId(classInfo.type));
			if(classInfo.repeat.equals("0")){
				setNoneRepeatView();
			}
			else
				if(classInfo.repeat.equals("1")){
					setRepeatWeeksView();
				}
				else{
					setRepeatWorkingDaysView();
				}
			label.setText(classInfo.timeStart.substring(0,5)+"-"+classInfo.timeEnd.substring(0, 5));
		}else{
			
		}
		showAssignmentList();
	}
	public void showAssignmentList(){
		AStudentDB myDB = new AStudentDB(act);
		ArrayList<Assignment> listAss = myDB.getStudentAssignmentList(id);
		if(listAss!=null&&listAss.size()==0){
			 ((RelativeLayout) act.findViewById(R.id.addNewAssignmentBtn)).setBackgroundResource(R.drawable.class_row_all);
		}
		ListView listAssView = (ListView) act.findViewById(R.id.list_ass);
		listAssView.setAdapter(new ListAssignmentAdapter(act,listAss,true));
		listAssView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				Intent intent = new Intent(act,ViewAssignmentDetail.class);
				Bundle bundle = new Bundle();
				ListAssignmentAdapter adapter = (ListAssignmentAdapter) arg0.getAdapter();
				ArrayList<Assignment> list = adapter.list;
				bundle.putString("id", list.get(arg2).id);
				bundle.putBoolean("isEdit", false);
				bundle.putBoolean("isCreateNew", false);
				bundle.putString("classId", classInfo.id);
				intent.putExtras(bundle);
				act.startActivityForResult(intent, Constants.ViewAssignmentDetailRequestCode);
			}
		});
		listAssView.setOnCreateContextMenuListener(new OnCreateContextMenuListener()  {
			@Override
			public void onCreateContextMenu(ContextMenu menu, View v,
					ContextMenuInfo menuInfo) {
				menu.add(0,Constants.CLASS_DETAIL_CONTEXTMENU_ADD_ASS,0, "Add new");
				menu.add(0,Constants.CLASS_DETAIL_CONTEXTMENU_EDIT_ASS,1, "Edit");
				menu.add(0,Constants.CLASS_DETAIL_CONTEXTMENU_DELETE_ASS,2, "Delete");
			}
       });
	}
	private void setRepeatWeeksView(){ 
		RelativeLayout rl = (RelativeLayout) act.findViewById(R.id.day);
		rl.removeAllViews();
		LayoutInflater li = LayoutInflater.from(act); 
		View view = li.inflate(R.layout.week_day_mini, rl); 
		int rlId = WeekDay.getDayId(classInfo.dateRepeat);
		rl = (RelativeLayout) act.findViewById(rlId);
		rl.setBackgroundResource(R.drawable.week_day_2);
	}
	private void setRepeatWorkingDaysView(){
		RelativeLayout rl = (RelativeLayout) act.findViewById(R.id.day);
		rl.removeAllViews();
		LayoutInflater li = LayoutInflater.from(act); 
		View view = li.inflate(R.layout.week_day_mini, rl); 
		for(int i=1;i<WeekDay.num.length-1;i++){
			rl = (RelativeLayout) act.findViewById(WeekDay.num[i]);
			rl.setBackgroundResource(R.drawable.week_day_2);
		}
	}
	private void setNoneRepeatView(){
		RelativeLayout rl = (RelativeLayout) act.findViewById(R.id.day);
		rl.removeAllViews();
		TextView tmp1 = new TextView(act);
		tmp1.setText(classInfo.dateStart);
		rl.addView(tmp1);
	}
	public void removeAssignment(String assId) {
		myDB.removeAssignment(assId);
	}
}
